<?php

namespace Kicker\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Kicker\MainBundle\Entity\Team;
use Kicker\MainBundle\Entity\Game;
use DateTime;

/*
 * 
 */
class GameController extends Controller
{
    /**
     * @Route("/", name="_game")
     */
    public function indexAction() {        
        return $this->redirect($this->generateUrl('_game_display'));
    }
    
    /**
     * @Route("/display", name="_game_display")
     * @Template()
     */
    public function displayAction() {  
       $em = $this->getDoctrine()->getManager();
       $games = $em->getRepository('KickerMainBundle:Game')->findAll();
       //print_r($teams); die();
       return $this->render('KickerMainBundle:Game:index.html.twig', array(
           'target'=>'game',
           'menu' => 'display',
           'games'=> $games
       ));
    }
    
    
    /**
     * @Route("/add", name="_game_add")
     */
    public function addGame() {
        $em = $this->getDoctrine()->getManager(); 
        if ( !empty($_POST) ) {
            $pk1 = $this->get('request')->request->get('team1');
            $pk2 = $this->get('request')->request->get('team2');
            $score = $this->get('request')->request->get('score1').'-'.$this->get('request')->request->get('score2');

            $team1 = $em->getRepository('KickerMainBundle:Team')->find($pk1);
            $team2 = $em->getRepository('KickerMainBundle:Team')->find($pk2);

            $game = new game();
            $game->addTeam($team1);
            $game->addTeam($team2);
            $game->setDate(new DateTime());
            $game->setResult($score);
            $game->setStatus(false);
            $em->persist($game);
            $em->flush();
            $em->clear();
        
            return $this->redirect($this->generateUrl('_game'));

        } else{
            $teams = $em->getRepository('KickerMainBundle:Team')->findAll();
            return $this->render('KickerMainBundle:Game:index.html.twig', array(
                'target'=>'game',
                'menu' => 'add',
                'teams'=> $teams
            ));    
        }
    }
    
    /**
     * @Route("/emails", name="_game_emails")
     */
    public function getPendingGames($em){
//        $em = $this->getDoctrine()->getManager(); 
        $games = $em->getRepository('KickerMainBundle:Game')->findAll();
        $msgs = array();
        foreach( $games as $game){
            if($game->getStatus() == false){
                $dest = array();
                foreach( $game->getTeams() as $team){
                    array_push($dest, $team->getEmail1());
                    array_push($dest, $team->getEmail2());
                    $game->setStatus(true);
                    $em->persist($game);
                    $em->flush();
                }
                $msgs[] = array(
                    'dest'=> $dest
//                    'game'=> $game
                );
            }
        }
        return $msgs;
    }
    
}
